package com.yun.algorithmproblem.leetcode;

import java.util.Stack;

public class Leetcode2645 {

    public int addMinimum(String word){
        char[] ch=word.toCharArray();
        int res=2;
        for (int i = 2; i <= ch.length; i++) {
            if(ch[i-1]>ch[i-2]){
                res--;
            }else {
                res+=2;
            }
        }
        return res;
    }
    public int addMinimum1(String word) {
        int res=0;
        char[] words=word.toCharArray();
        Stack<Character> stack=new Stack<>();
        for (int i = 0; i < words.length; i++) {
            char c=words[i];
            if(c=='a'){
                if(stack.isEmpty()){
                    stack.push(c);
                }else {
                    res=res+3-stack.size();
                    stack.clear();
                    stack.push(c);
                }
            }else if(c=='b'){
                if(stack.isEmpty()){
                    stack.push('a');
                    stack.push(c);
                    res++;
                }else if (stack.peek()=='a'){
                    stack.push(c);
                }else {
                    res=res+2;
                }
            }else if(c=='c'){
                res=res+2-stack.size();
                stack.clear();
            }
        }
        if(!stack.isEmpty()){
            if(stack.peek()=='a'){
                res+=2;
            }else {
                res+=1;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        String word="abcb";
        int res=new Leetcode2645().addMinimum(word);
        System.out.println(res);
    }
}
